using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SwinMessengerLibrary;

namespace SwinMessenger
{
    public partial class Main : Form
    {
        RosterPanel _RosterPanel;
        ServerPanel _ServerPanel;

        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            _ServerPanel = new ServerPanel();

            _ServerPanel.Server.OnLogin += new EventHandler(_Server_OnLogin);
            _ServerPanel.Server.OnAuthError += new EventHandler(_Server_OnAuthError);

            _ServerPanel.Anchor = AnchorStyles.Left | AnchorStyles.Right;

            this.Controls.Add(_ServerPanel);
        }

        void _Server_OnAuthError(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new EventHandler(_Server_OnAuthError),
                    new object[] { this, e });
                return;
            }
            MessageBox.Show("Authentication Error");
            _ServerPanel.PasswordTextBox.Focus();
            _ServerPanel.PasswordTextBox.SelectionStart = 0;
            _ServerPanel.PasswordTextBox.SelectionLength =
                _ServerPanel.PasswordTextBox.Text.Length;
        }

        void _Server_OnLogin(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new EventHandler(_Server_OnLogin),
                    new object[] { this, e });
                return;
            }
            _ServerPanel.Hide();

            _RosterPanel = new RosterPanel(_ServerPanel.Server.Roster);
            _RosterPanel.Dock = DockStyle.Fill;
            _RosterPanel.Disposed += new EventHandler(_RosterPanel_Disposed);

            this.Controls.Add(_RosterPanel);
        }

        void _RosterPanel_Disposed(object sender, EventArgs e)
        {
            _ServerPanel.Show();
        }


        private void rosterPanel1_Load(object sender, EventArgs e)
        {

        }

        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }
    }
}